package cn.tedu.v7;

import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
    private Socket socket;
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 6666;
    public BufferedReader br;
    private PrintWriter pw;


    public Client() {
        try {
            socket = new Socket(HOST, PORT);
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (NullPointerException e) {
            System.out.println("友情提示：IP传入错误导致连接失败");
        } catch (UnknownHostException e) {
            System.out.println("友情提示：IP传入错误");
        } catch (ConnectException e) {
            System.out.println("客户端连接失败：建议检查IP或者端口号");
        } catch (IOException e) {
            System.out.println("客户端链接服务器失败，提示：检查IP/端口");
        }
    }

    private String ip;
    public void start() {
        System.out.println("~~~欢迎使用哈阳的聊天室~~~");
        Scanner input=new Scanner(System.in);
        try {
            String serverMessage=br.readLine();
            System.out.println(serverMessage);
            String username=input.nextLine();
            pw.println(username);
            serverMessage= br.readLine();
            System.out.println(serverMessage);
            String password=input.nextLine();
            pw.println(password);
            serverMessage=br.readLine();
            System.out.println(serverMessage);
            if (serverMessage.startsWith("登陆成功")){
            //    System.out.println("请开始聊天：");
                chat();
            }
            //关闭通讯---exit

            if (serverMessage.equalsIgnoreCase("exit")){
                System.exit(0);
            }

        }catch (IOException e){
            System.out.println("客户端断开连接了！");
        }
        finally {
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            if (pw != null) {
                pw.close();
            }
            if (socket != null && !socket.isClosed()) {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("通讯关闭失败");
                }
            }
        }
    }
    private void chat(){
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("客户端请输入：");
                    System.out.println("服务器端说：" + message);
                  //  System.out.println("客户端请输入:"+"输入exit离开聊天室");  //? 这句貌似没有什么用
                }
            } catch (IOException e) {
                System.out.println("服务器端读入失败");
            }
        }).start();

        Scanner input=new Scanner(System.in);  //这里已经创建了
        while (true) {
            System.out.println("客户端请输入：");
            String line = input.nextLine();
            pw.println("客户端说：" + line);

            //关闭通讯---exit
            String message=input.nextLine();  //Scanner 不需要私有化 哪里使用 哪里创建
            if (message.equalsIgnoreCase("exit")){
                pw.println(message);
                break;
            }
        }
    }
    public static void main(String[] args) {
        new Client().start();
    }
}
/**
 * 客户端与服务器端的读写都要写退出，你这个两个问题：1.服务器端没有写出给客户端提示，输入exit退出
 *  在线用户列表就没有写对哦
 *
 */